package com.ITArmy.controller.admin;

import com.ITArmy.beans.Order;
import com.ITArmy.common.ServerResponse;
import com.ITArmy.controller.base.BaseController;
import com.ITArmy.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Api(value = "后台订单接口层次", tags = "后台订单接口层次Controller层")
@Controller
@RequestMapping("/admin/Order")
public class AdminOrderController extends BaseController {

    @RequestMapping("/addOrderByBack")
    @ResponseBody
    @ApiOperation(value = "添加订单信息" ,
            httpMethod = "POST", response = ServerResponse.class)
    public ServerResponse addOrderByBack(Order order){
        order.setsTime(new Date());
        return orderService.addOrderByBack(order);
    }

    @RequestMapping("/list")
    @ApiOperation(value = "显示所有订单信息",
            httpMethod = "POST", response = ServerResponse.class)
    public String list(@RequestParam(value = "page", defaultValue = "1") int page, Model model){
        PageHelper.startPage(page,9);
        List<Order> orders=orderService.orderList();
        PageInfo PageInfo = new PageInfo<>(orders,9);
        model.addAttribute("orderList",PageInfo);
        return "order";
    }

    @RequestMapping("/update")
    @ResponseBody
    @ApiOperation(value = "修改订单信息",notes = "订单id不能改，类型用固定选择（连载，提现，充会员）,修改前先通过订单号查找订单信息显示出来",
            httpMethod = "POST", response = ServerResponse.class)
    public ServerResponse update(Order order){
        return orderService.updateOrder(order);
    }

    @RequestMapping("/findOrderByOId")
    @ResponseBody
    @ApiOperation(value = "通过订单号查找订单信息",
            httpMethod = "POST", response = ServerResponse.class)
    public ServerResponse findOrderByOId(Long oid){
        return orderService.findOrderByOId(oid);
    }

    @RequestMapping("/findOrderByUId")
    @ResponseBody
    @ApiOperation(value = "通过用户id查找订单信息",
            httpMethod = "POST", response = ServerResponse.class)
    public ServerResponse findOrderByUid(Long uid){
        return orderService.findOrderByUid(uid);
    }

    @RequestMapping("/deleteOrder")
    @ResponseBody
    @ApiOperation(value = "通过订单号删除订单信息",
            httpMethod = "DELETE", response = ServerResponse.class)
    public ServerResponse deleteOrder(Long oid){
        return orderService.deleteOrder(oid);
    }

    @RequestMapping("/deleteMany")
    @ResponseBody
    @ApiOperation(value = "通过订单号批量删除订单信息",
            httpMethod = "DELETE", response = ServerResponse.class)
    public ServerResponse deleteMany(String ids){
        String[] id=ids.split(",");
        return orderService.deleteManyByIds(id);
    }

    @RequestMapping("/findOrderBySId")
    @ResponseBody
    @ApiOperation(value = "通过连载id查找订单信息",
            httpMethod = "POST", response = ServerResponse.class)
    public ServerResponse findOrderBySId(Long sid){
        return orderService.findOrderBySid(sid);
    }
}
